package test4;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class ServerDemo {
    public static void main(String[] args) throws IOException {
        System.out.println("请等待客户端先发言...");
        Scanner sc = new Scanner(System.in);
        ServerSocket ss = new ServerSocket(8888);
        lo:while (true) {
            Socket accept = ss.accept();
            BufferedReader br = new BufferedReader(new InputStreamReader(accept.getInputStream()));
            String line;
            while ((line = br.readLine()) != null) {
                if ("exit".equalsIgnoreCase(line)){
                    accept.close();
                    ss.close();
                    break lo;
                }
                System.out.print("客户端-回复: ");
                System.out.println(line);
            }
            accept.shutdownInput();

//            输入
            System.out.println("服务端-请输入:");
            String message = sc.next();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
            bw.write(message);
            bw.flush();
            accept.shutdownOutput();
            accept.close();
        }
//        ss.close();
    }
}
